package com.manager.devicemanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manager.devicemanager.common.DataSourceNameEnum;
import com.manager.devicemanager.entity.Testcase;
import com.manager.devicemanager.service.ITestcaseService;
import com.manager.devicemanager.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.manager.devicemanager.common.Result;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/testcase")
public class TestcaseController {
    @Autowired
    private ITestcaseService testcaseService;

    /**
     * 带远程条件筛选的分页查询接口
     * @param testcasePageVO 分页查询条件对象，包含页码、每页数量和查询条件
     * @return 分页查询结果
     */
    @Operation(summary = "带远程条件筛选的分页查询")
    @PostMapping("/page")
    public Result<IPage<Testcase>> page(@Parameter(description = "分页查询条件对象") @RequestBody PageVO<Testcase> testcasePageVO) {
        int pageNum = testcasePageVO.getPageNum();
        int pageSize = testcasePageVO.getPageSize();
        Testcase testcase = testcasePageVO.getCondition();
        LambdaQueryWrapper<Testcase> queryWrapper = new LambdaQueryWrapper<>(testcase);
        IPage<Testcase> testcasePage = testcaseService.Bypage(testcasePageVO.getType().equals("ic")?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName()+testcasePageVO.getDatasourceindex(), new Page<>(pageNum, pageSize), testcase);
        return Result.success(testcasePage);
    }
    @PostMapping("/default")
    @Operation(summary = "获取默认测试用例")
    public Result<Testcase> getByDefault(@Parameter(description = "")@RequestBody PageVO<Testcase>  testcasePageVO) {
        LambdaQueryWrapper<Testcase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Testcase::getTestCaseName, "default");
        Testcase testcase = testcaseService.getByDefault(testcasePageVO.getType().equals("ic")?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName()+testcasePageVO.getDatasourceindex(), queryWrapper);
        return Result.success(testcase);
    }
    @PostMapping("/list")
    @Operation(summary = "获取测试用例列表")
    public Result<List<Testcase>> getList(@Parameter(description = "")@RequestBody PageVO<Testcase>  testcasePageVO) {
        List<Testcase> testcases = testcaseService.getList(testcasePageVO.getType().equals("ic")?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName()+testcasePageVO.getDatasourceindex());
        return Result.success(testcases);
    }

}
